<?php
/**
* @package Sample MiaCMS REST Client (SMRC)
* @author Chad Auld and Ozgur Cem Sen (code@brilaps.com)
* @copyright Brilaps, LLC (http://brilaps.com)
* @link http://brilaps.com || http://wiki.brilaps.com
* @license http://www.opensource.org/licenses/gpl-license.php GNU/GPL v.2.
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Sample MiaCMS MOStlyREST Client (SMRC)</title>
	<link rel="stylesheet" href="css/thickbox.css" type="text/css" media="screen" />
	<link rel="stylesheet" href="css/smrc.css" type="text/css" media="screen" />
	<script type="text/javascript" src="js/jquery-1.2.3.min.js"></script>
    <script type="text/javascript" src="js/thickbox-compressed.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $(function(){
                //Fix for target="_blank"
                $("a[@rel~='external']").click(function(){
                    window.open($(this).attr("href"));
                    return false;
                });
            });
        });
    </script>
</head>
<body>
<h1>Sample MiaCMS REST Client</h1>
<p class="smrc-desc">This is a quick and dirty example (requires PHP5+) of how one might use the new <a rel="external" href="http://wiki.brilaps.com/wikka.php?wakka=MOStlyREST">MOStlyREST</a> RESTful API to create unique and exciting external 
services and applications for the <a rel="external" href="http://miacms.org">MiaCMS</a> (and <a rel="external" href="http://source.mambo-foundation.org">Mambo</a>/<a rel="external" href="http://www.joomla.org/">Joomla</a>) CMSs.  
This client simply calls the MiaCMS (or Mambo/Joomla) website defined in the configuration file and retrieves the top 5 ranked articles.  It then passes 
the content of these articles along to the <a rel="external" href="http://developer.yahoo.com/search/content/V1/termExtraction.html">Yahoo! Term Extractor web service</a> 
which in turn sends back what it feels are the key terms within the article.  Once we have the key terms they are sent to the <a rel="external" href="http://www.flickr.com/services/api/">Flickr web service</a> 
where a tag search is done for photos tagged with our key words.  We display these related photos and key words below the original content and use 
a thickbox effect to show larger versions of the thumbnails (Note: Like we said quick and dirty.. meaning we are not verifying all the image paths so 
some may not work, but most should.).  More information on Brilaps and/or MOStlyREST can be found <a rel="external" href="http://brilaps.com">here</a>.
<br /><br />
Dowload your own copy of SMRC to play and/or learn from <a rel="external" href="http://code.google.com/p/mostlyrest/downloads/list">here</a>.
</p>
<hr />
<?php
function curlMe($requestUrl) {
	$ch = curl_init($requestUrl);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return as string
	curl_setopt($ch, CURLOPT_HEADER, 0);
	$results = curl_exec($ch);
	curl_close($ch);
	return $results;
}

require_once("config.inc.php");
$miaRequestUrl = $RestBaseUrl.'/index2.php?option=com_rest_content&no_html=1&task=dataRetrival';
$miaRequestParams = array('type'=>'rank', 'results'=>'5', 'output'=>'php', 'appid'=>$appid);
foreach($miaRequestParams as $key=>$value) {
	$miaRequestUrl .= '&'.$key.'='.$value;
}

$top5 = curlMe($miaRequestUrl);
$top5Objs = unserialize($top5);
foreach($top5Objs as $top5Obj) {
	//Output the title and html of the content
	echo '<div class="smrc-item">';
	echo '<h2>Article: '.$top5Obj->title.'</h2>';
	echo $top5Obj->introtext.'<br />'.$top5Obj->fulltext.'<br />'; 
	
	//Combine intro/main text and strip html
	$combinedText = strip_tags($top5Obj->introtext.' '.$top5Obj->fulltext);
	//Pass stripped text off to the Yahoo term extractor to find key terms
	$YahooTEUrl = $yahooTermExtractUrl.'appid='.$yahooApiKey.'&context='.rawurlencode(utf8_encode($combinedText)).'&output=php';
	$keyTermResults = unserialize(curlMe($YahooTEUrl));
	$keyTerms='';
	for($i=0;$i<count($keyTermResults['ResultSet']['Result']);$i++) {
		if ($i>0) {
			$keyTerms .= ',';
		}
		$keyTerms .= rawurlencode($keyTermResults['ResultSet']['Result'][$i]);
	}
	
	//Output key terms for reference
	echo '<h3>Term Extractor Identified Key Terms</h3>';
	if ($keyTermResults) {
		echo '<ul>';
		for($i=0;$i<count($keyTermResults['ResultSet']['Result']);$i++) {
			echo '<li>'.$keyTermResults['ResultSet']['Result'][$i].'</li>';
		}
		echo '</ul>';
	}
	
	//Pass the key terms off to flickr for some related images
	$flickrSearchParams = array('method'=>'flickr.photos.search', 'format'=>'rest', 'sort'=>'relevance',
								'license'=>5, 'privacy_filter'=>1, 'api_key'=>$flickApiKey, 'tags'=>$keyTerms);
	$flickrSearchUrl = $flickrApiUrl;
	foreach($flickrSearchParams as $key=>$value) {
	 	$flickrSearchUrl .= '&'.$key.'='.$value;
	}
	$flickrSearchResults = new SimpleXMLElement(curlMe($flickrSearchUrl));
	
	//Identify the images, show thumbs, and allow click for large images
	$imgcount = 0; //We'll just do 20 for each article
	foreach ($flickrSearchResults->xpath('//photo') as $photo) {
	    $imgcount++;
	    if ($imgcount>20) {
	        break;
	    }
		$tImage = '<img src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg"'
		. ' alt="'.htmlspecialchars($photo['title']).'" />';
		$oImage = 'http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_o.jpg';
		echo "<a class=\"thickbox\" title=\"".htmlspecialchars($photo['title'])."\" href=\"$oImage\">$tImage</a>";
	}
	echo '</div><hr />';
}
?>

</body>
</html>